package com.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.document.common.annotation.SysLog;
import com.document.common.exception.SoException;
import com.document.common.utils.DataResult;
import com.document.entity.request.FileNotesRequest;
import com.document.entity.response.FileNotesResponse;
import com.document.service.iservice.FileNoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/filenote")
@Api(tags = "文件")
public class FileNoteController {
	@Autowired
	private FileNoteService FileNoteService;

	@PostMapping(value = "/list")
	@ApiOperation(value = "列表", httpMethod = "POST")
	public DataResult list(@RequestBody FileNotesRequest request) {
		IPage<FileNotesResponse> list = FileNoteService.queryAll(request);
		return DataResult.success(list);
	}
	@PostMapping(value = "/insert")
	@ApiOperation(value = "添加笔记", httpMethod = "POST")
	public DataResult insert(@RequestBody FileNotesRequest request) {
		try {
			int count = FileNoteService.save(request);
			if(count>0){
				return DataResult.success();
			}else {
				return DataResult.error("保存注释！");
			}
		}catch(SoException exception){
			return DataResult.error(exception.getMsg());
		}
	}
	/**
	 * 修改文件
	 */
	@PostMapping("/update")
	@SysLog(value = "修改注释")
	@ApiOperation(value = "修改注释", httpMethod = "POST")
	public DataResult update(@RequestBody FileNotesRequest request){
		try {
			int count = FileNoteService.update(request);
			if(count>0){
				return DataResult.success();
			}else {
				return DataResult.error("修改注释失败！");
			}
		}catch(SoException exception){
			return DataResult.error(exception.getMsg());
		}
	}

	/**
	 * 删除文件
	 * @param ids
	 * @return
	 */
	@PostMapping("/delete")
	@SysLog(value = "删除笔记")
	@ApiOperation(value = "删除注释", httpMethod = "POST")
	public DataResult delete(@RequestBody String[] ids){
		try {
			FileNoteService.deleteById(ids);
			return DataResult.success();
		}catch(SoException exception){
			return DataResult.error(exception.getMsg());
		}
	}
}
